import 'dart:io';
import 'dart:typed_data';

import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:cryptox/cryptox.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion = "ass";
    // Platform messages may fail, so we use a try/catch PlatformException.
    print("use flutter md5.....");

    String ss = "1234569875623";
    print(ss + ",md5: " + await Crypto.md5String(ss)); // f1ad91adfb7e2ee13cf92851555cc050

    Uint8List uint8list = Uint8List.fromList([0,1,2,2,5,5,5,6,5,6,4,46,69,55,127,126,10]);
    print("byte md5: " + await Crypto.md5Bytes(uint8list)); //30bed9bc4caaf297d9a0e2c5d5212c5d

    String sss = await Crypto.aesEncrypt("123456789您好啊flutter", "qwertyuioqwertyuio", hex: false);
    print("aes enc:" + sss); // Ta9KLqsDxoOPz2Yk1iztKrfTAmh+ZiWru2cdkQqyobcdIlfbvlL3E1A=
    print("aes dec:" + await Crypto.aesDecrypt(sss, "qwertyuioqwertyuio", hex: false));

    sss = await Crypto.aesEncrypt("123456789您好啊flutter", "qwertyuioqwertyuio");
    print("aes enc:" + sss); // 4daf4a2eab03c6838fcf6624d62ced2ab7d302687e6625abbb671d910ab2a1b71d2257dbbe52f71350
    print("aes dec:" + await Crypto.aesDecrypt(sss, "qwertyuioqwertyuio"));

    sss = await Crypto.hmacSHA256("123456789您好啊flutter", "qwertyuio");
    print("hmac-sha256: " + sss); //c9df3f648a4d888fc17ac5cee185a953aee0080c1fcfe6ea57a848ca0b548cfe

    sss = Crypto.HmacSHA256("123456789您好啊flutter", "qwertyuio");
    print("hmac-sha256: " + sss);

    sss = Crypto.SHA256("123456789您好啊flutter");
    print("sha256: " + sss);//cadfaa0634dc4a0e0a6bb76075cc36b2e12906725f0614872f0642371116305d

    sss = await Crypto.sha256String("123456789您好啊flutter");
    print("sha256: " + sss);

    sss = await Crypto.sha256File("/sdcard/GZYProFile/VideoSaveDir/xjlx20220105091814.mp4");
    print("sha256: " + sss);

    String publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOWUTRP5cvR1hCPI+l+FPW0ixutaYou7z6LQTYQYZkCHE46+FP7oD/SJNC4Ty3uPr5cweNvqWwbudDXOVlOvEJsQY1ocogJUnpcrcjItVwpI5wdX1SEsRaqrayzYfRlPVeITr/hh6DSvpIpygtuCCkUnq7osYiCatRwx/L2Qn6QwIDAQAB';
    String privateKey =
      'MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI5ZRNE/ly9HWEI8j6X4U9bSLG61pii7vPotBNhBhmQIcTjr4U/ugP9Ik0LhPLe4+vlzB42+pbBu50Nc5WU68QmxBjWhyiAlSelytyMi1XCkjnB1fVISxFqqtrLNh9GU9V4hOv+GHoNK+kinKC24IKRSeruixiIJq1HDH8vZCfpDAgMBAAECgYAEOwBYRnWie/ifBOuKyRdwxg+rY9TVPay1mkeCrjVyXQZEZOcHnmgROvs8nLLWjcK251jDn471o3rv0RLbA2y5y8Mwculh1XrEC8LLVnW2TmbbgtpRVf+ckyw+d3ZCz+VsK+n1aBHziHCcqRzHDSXyJ0BhxXuVtEElWI/m0iUHdQJBAL8I4cGgYbWAIiqgjpbLCDQmg9BrWws6h74wXU5cpNaj+c628OOju3CEm5f3V40wNOAdI/cPpyT6T3asCx5BjG0CQQC+weCNeM9519oc7p+iGjYOs01Gp7ZzyKDIYIcI28bsvKl0FLbRWwnIifTprHHdXYqCjdOtKhSoCjBhIlRuuhNvAkBeyAVakDxqVPyesbKNVobneQSFb/5n/85BqrVgn86ZQBo9Wb8C8sWY+fJI38tsnbJeT5b8iaOgUSED/DA3/sWxAkBoM9BOd6gejuL+2vsoudi9vwR/jC4GoZny24foNuY7e/O1DXuwcWKuwW2gGCs7GVYIayEufsY55Rb4eKBWvB7RAkAEk7dTykHtZjVnd8bn+9qmIYRHTTqWH78rxnUXX3XNFv/idW7V/uHsdb0gPORn+RMyyrlXNsmrC5RomTYZq3C6';
    sss = await Crypto.rsaEncrypt("123456789您好啊flutter", publicKey, hex: false);
    print("rsa enc: " + sss);
    sss = await Crypto.rsaDecrypt(sss, privateKey, hex: false);
    print("rsa dec: " + sss);

    ss = "/sdcard/GZYNotaryFile/Screen/20210311-164209/sjlp20210311164209.mp4";
    var file = File(ss);
    if(file.existsSync()) {
      var start = new DateTime.now().millisecondsSinceEpoch;
      print(ss + ",md5: " + await Crypto.md5File(ss)); //1G文件大概花费10s
      var end = new DateTime.now().millisecondsSinceEpoch;
      print(end - start);

      start = new DateTime.now().millisecondsSinceEpoch;
      print(await md5
          .bind(file.openRead())
          .first); //使用官方的文件md5算法，1G文件大概花费了48s
      end = new DateTime.now().millisecondsSinceEpoch;
      print(end - start);
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}
